#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<sys/types.h> 
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>


#define null		NULL

int init ();

/*-- Estas son las funciones segundas --*/
void error (char* msg){
  printf ("\e[1;33m!-- ERROR -> \e[1;34m%s\e[0m\e[1;33m --!\e[0m\n", msg);
}

void debug (char* msg, char* func, char* page){
  printf ("\e[1;35m[ DEBUG: %s: %s: %s ]\e[0m\n", page, func, msg);
}
int main(int argc, char** argv){
  
  if(argc<2){
    printf ("Usage: %s <mood> [options [...]]\n", argv[0]);
    exit (1);
  }
  
  int n = atoi (argv[1]);
  
  if(n == 0){
    int r = init ();
    return r;
  }
  
  return 0;

}

int init (void){

     debug ("initial.Directive begun", "init", "directives/initial/init.c");
     int sockfd, portno, n;
     struct sockaddr_in serv_addr;
     struct hostent *server;

     char buffer[256];
     sockfd = socket (AF_INET, SOCK_STREAM, 0);
     if(sockfd < 0) 
        error ("ERROR opening socket");
     server = gethostbyname ("127.0.0.1");

     if(server == null){
       error ("No such host!");
       return 1;
     }

     
     bzero((char *)&serv_addr, sizeof (serv_addr));
     portno = 5000;
     serv_addr.sin_family = AF_INET;
     bcopy ((char*)server->h_addr,
       (char*)&serv_addr.sin_addr._s_addr,
       server->h_length);
     
     serv_addr.sin_port = htons (portno);
     if(connect (sockfd, (struct sockaddr*)&serv_addr, sizeof (serv_addr)) < 0){
       error ("Error connecting : initial.Directive");
     }

     n = write (newsockfd, "0x0BEBE", 7);
     n = write (newsockfd, "VAIS initial.Directive has been defined.  I will learn about your computer now...", 82);
     if(n < 0) error("ERROR reading from socket");
     bzero (buffer, 256);
     n = read (newsockfd, buffer, 255);
     if(strcmp (buffer, "0x00") == 0){
       n = write (newsockfd, "0x0E", 4);
     }
     if(n < 0) error ("ERROR writing to socket");
     close (newsockfd);
     close (sockfd);
     return (null); 
}
